#include <boost/program_options.hpp>
#include <string>
#include <iostream>
#include <cstdlib>

#include "vector.h"
#include "jarvis.h"
#include "input.h"


#include <boost/shared_ptr.hpp>
#include <boost/foreach.hpp>
#define foreach BOOST_FOREACH

namespace po = boost::program_options;

using namespace Tujchi;

std::vector<Vector> test(void) {
	std::vector<Vector> points;
	for(int i =0; i < 100; i++) {
		int random = rand()%10;
		int random2 = rand()%10;
		points.push_back(Vector(random, random2));
	}
	return points;
}

int main(int ac, char** av) {
	bool file = false;
	std::string filename;
	try {
		po::positional_options_description p;
		p.add("input-file", -1);

		po::options_description desc("TUJCHI - Allowed options");
		desc.add_options()
				("help", "produce help message")
				("input-file", po::value< std::vector<std::string> >(), "input file")
		;

		po::variables_map vm;
		po::store(po::command_line_parser(ac, av).
          options(desc).positional(p).run(), vm);

		po::notify(vm);    

		if (vm.count("help")) {
				std::cout << desc << "\n";
				return 0;
		}
		if (vm.count("input-file")) {
			filename = (vm["input-file"].as< std::vector<std::string> >())[0];
			file = true;
		}

		if(file) {
			Input input(filename);	
			std::vector<Tujchi::Vector> result;
			std::vector< std::vector<Tujchi::Vector> > points = input.parse();
			for(unsigned int i = 0;i < points.size(); i++) {
				Tujchi::Jarvis jarvis(points.at(i));
				result  =  jarvis.convex_hull();
			}
			input.output("output.obj", result);
		}
		else {
				std::cout << desc << std::endl;
		}
	}
	catch(std::exception& e) {
		 std::cerr << "error: " << e.what() << std::endl;
		 return 2;
	}
	catch(...) {
		std::cerr << "Unespected error!" << std::endl;
		return 3;
	}

	return 0;
}
